﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Evogame.Settings;
using Microsoft.Win32;
using EvoGame.Ceeforee.Helpers;
using System.Security.AccessControl;

namespace EvoGame.Ceeforee
{
	public partial class ModSelector : Form
	{
		private MainWin mainWin;
		private List<RadioButton> radioButtons = new List<RadioButton>();

		public ModSelector()
		{
			InitializeComponent();
		}

		public ModSelector(MainWin win)
		{
			mainWin = win;
			InitializeComponent();
		}

		private void ModSelector_Load(object sender, EventArgs e)
		{
			civMode.SelectedIndex = (int)mainWin.Mode;
			mainWin.Hide();
			Draw();
		}

		private void Draw()
		{
			DirectoryInfo[] dirs = Helper.GetMods(mainWin.Mode);
			radioButtons.Clear();
			panel1.Hide();
			for (int cnt = 0; cnt < dirs.Length; cnt++)
			{
				RadioButton radbut = new RadioButton();
				radbut.AutoSize = true;
				if (Math.Round(8 % (1 + (float)cnt)) < 8)
				{
					radbut.Location = new System.Drawing.Point(0, (32 * cnt));
				}
				else if (Math.Round(16 % (1 + (float)cnt)) < 16)
				{
					radbut.Location = new System.Drawing.Point(175, (32 * (cnt - 8)));
				}
				else if (Math.Round(24 % (1 + (float)cnt)) < 24)
				{
					radbut.Location = new System.Drawing.Point(350, (32 * (cnt - 16)));
				}
				else
				{
					radbut.Location = new System.Drawing.Point(525, (32 * (cnt - 24)));
				}
				radbut.Name = dirs[cnt].Name;
				radbut.Size = new System.Drawing.Size(85, 17);
				radbut.TabIndex = cnt;
				radbut.TabStop = true;
				radbut.Text = dirs[cnt].Name;
				radbut.UseVisualStyleBackColor = true;
				radioButtons.Add(radbut);
			}
			panel1.Controls.Clear();
			foreach (RadioButton but in radioButtons)
			{
				panel1.Controls.Add(but);
			}
			panel1.Show();
		}

		private void civMode_State_Changed(object sender, EventArgs e)
		{
			mainWin.Mode = (CivX)civMode.SelectedIndex;
			mainWin.LoadedMod.SetDir("CivX", Helper.CivDir(mainWin.Mode));
			Draw();
		}

		private void okButton_Click(object sender, EventArgs e)
		{
			string modDir = null;
			for (int q = 0; q < radioButtons.Count; q++)
			{
				if (radioButtons[q].Checked)
				{
					modDir = this.radioButtons[q].Text;
					break;
				}
			}
			if (modDir != null)
			{
				mainWin.LoadedMod.SetMod(modDir);
				mainWin.LoadSettings();
				mainWin.civDB = C4EDB.Load(mainWin);
				mainWin.LoadCivilizations();
				mainWin.Debug = mainWin.civDB.GetTXTKey("TXT_KEY_AND");
				mainWin.Show();
				Close();
			}
			else
			{
				string message = "No mod selected. Please select a mod.";
				mainWin.Debug = "Error Displayed:" + Environment.NewLine + message;
				MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void newButton_Click(object sender, EventArgs e)
		{
			string mod;
			InputBox.Show("Please enter a name for your mod:", "New Mod", "Civilization4Mod1", out mod);
			mainWin.LoadedMod.SetMod(mod);
			foreach (KeyValuePair<string,string> kvp in mainWin.LoadedMod.Dir["Mod"])
			{
				Directory.CreateDirectory(kvp.Value);
			}
			mainWin.LoadSettings();
			mainWin.SaveSettings();
			mainWin.civDB = C4EDB.Load(mainWin);
			mainWin.Debug = mainWin.civDB.GetTXTKey("TXT_KEY_AND");
			mainWin.Show();
			Close();
		}
	}
}
